import api from "../../../api/api.js"
import consts from "../../../utils/consts.js"
import courseUtil from "../../../utils/course.js"

Page({
  data: {
    weeks: [],
    times: [],
    course: {}
  },
  onLoad: function (option) {
    var id = option.courseId
    this.setData({
      weeks: consts.weeks,
      course: courseUtil.getCourseById(id)
    })

    var that = this
    api.getCourse({
      method: 'get',
      data: {
        id: id
      },
      success: function (resp) {
        if (resp.code == 0) {
          that.setData({
            course: courseUtil.parseCourse(resp.data)
          })
        } else {
          wx.showToast({
            title: resp.message,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },
  bindLocationTap: function () {
    wx.openLocation({
      name: this.data.course.l_name,
      address: this.data.course.l_address,
      latitude: this.data.course.l_latitude,
      longitude: this.data.course.l_longitude
    })
  },
  /**
   * 修改课程
   */
  bindUpdateCourseTap: function () {
    wx.navigateTo({
      url: '/pages/my/course-edit/index?id=' + this.data.course.id,
    })
  },
  /**
   * 删除课程
   */
  bindDeleteCourseTap: function () {
    api.delCourse({
      method: 'get',
      data: {
        openid: getApp().globalData.openid,
        id: this.data.course.id
      },
      success: function (resp) {
        if (resp.code == 0) {
          courseUtil.setCourses(courseUtil.parseCourses(resp.data))
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000
          })
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showToast({
            title: '删除失败',
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },
  /**
   * 分享课程
   */
  bindShareCourseTap: function () {
    this.onShareAppMessage()
  },

  onShareAppMessage: function () {
    return {
      title: this.data.course.title,
      path: '/pages/my/course-item/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})